package com.shop.trade.api.controller;

import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderBaseInput;
import com.shop.common.model.param.trade.OrderDetailInput;
import com.shop.common.model.result.trade.OrderBaseOutput;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.trade.api.api.IOrderApi;
import com.shop.trade.api.service.IOrderSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月07日  23:25
 */
@RestController
@CrossOrigin
public class OrderController extends BaseController implements IOrderApi {
    @Autowired
    SessionUtils sessionUtils;
    @Autowired
    IOrderSvc orderSvc;

    @Override
    public String initOrder(@RequestBody Map<String, Integer> map, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        Integer itemId = map.get("itemId");
        ServiceBean<Integer> serviceBean = orderSvc.initOrder(itemId, userId);
        return returnSvc(serviceBean);
    }

    @Override
    public String initOrderForShopCart(@RequestBody Map<String, List<Integer>> map, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        List<Integer> itemIds = map.get("itemIds");
        ServiceBean<List<Integer>> serviceBean = orderSvc.initOrderByShopCart(itemIds, userId);
        return returnSvc(serviceBean);
    }

    @Override
    public String qryOrders(OrderBaseInput orderBaseInput, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        if (orderBaseInput.getMchtId() != null) {
            orderBaseInput.setMchtId(userId);
        } else {
            orderBaseInput.setBuyerId(userId);
        }
        orderBaseInput.setOrderStatus(orderBaseInput.getOrderStatus() == null ||
                orderBaseInput.getOrderStatus() == -1 ? null : orderBaseInput.getOrderStatus());
        ServiceBean<List<OrderBaseOutput>> serviceBean = orderSvc.qryOrders(orderBaseInput);
        return returnSvc(serviceBean);
    }

    @Override
    public String saveOrders(@RequestBody Map<String, List<OrderBaseInput>> inParam, boolean integral, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        List<OrderBaseInput> orderList = inParam.get("orders");
        ServiceBean<BigDecimal> serviceBean = orderSvc.saveOrder(orderList, integral, userId);
        return returnSvc(serviceBean);
    }

    @Override
    public String deleteOrder(String orderId, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(orderSvc.delOrder(Integer.valueOf(orderId), userId));
    }

    @Override
    public String modeOrder(@RequestBody OrderBaseInput orderBaseInput, HttpServletRequest request) {
        return returnSvc(orderSvc.modOrder(orderBaseInput));
    }

    @Override
    public String cancelOrder(@RequestBody Map<String, String> inParam, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        String reason = inParam.get("reason");
        String orderIdstr = inParam.get("orderId");
        return returnSvc(orderSvc.cancelOrder(Integer.valueOf(orderIdstr), reason, userId));
    }

    @Override
    public String validateOrder(Integer orderId, Integer state) {
        return returnSvc(orderSvc.validateOrder(orderId, state));
    }

    @Override
    public String checkOrder(String idStr, String state, String type, String comment, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(orderSvc.checkOrder(Integer.valueOf(idStr), Integer.valueOf(state), type, comment, userId));
    }

    @Override
    public String sendOrder(String idStr, String expressNo, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(orderSvc.sendOrder(Integer.valueOf(idStr), expressNo));
    }

    @Override
    public String endOrder(@RequestBody OrderBaseInput input, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(orderSvc.endOrder(input));
    }

    @Override
    public String qryOrderDetail(OrderDetailInput orderDetailInput, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        orderDetailInput.setBuyerId(userId);
        return returnSvc(orderSvc.qryOrderDtail(orderDetailInput));
    }

    @Override
    public String qryOrderItemCount(@PathVariable("userId") Integer userId){
        return returnData(orderSvc.qryOrderItemCount(userId));
    }

}
